import { checkToken, getUserToken } from "../service/index";
import { createModalComp } from './comp';

// 检测token的登录态，返回 true / false
export const checkHasLogined = async () => {
  const token = uni.getStorageSync('token');
  if (!token) return false;
  try {
    const res: any = await checkToken(token)
    if (!res.data?.isLogined) {
      uni.removeStorageSync('token');
      return false;
    }
    return true
  } catch (error) {
    uni.removeStorageSync('token');
    return false;
  }
}

// 微信token登录
export const login = async (page: any) => {
  uni.login({
    success: res => {
      getUserToken(res.code).then((res: any) => {
        if (res.status != "success") {
					createModalComp({ title: "无法登录", content: res.msg, showCancel: false });
          return;
        }
        uni.setStorageSync('token', res.data.token)
        uni.setStorageSync('utid', res.data.utid)
        if ( page ) page.onShow()
      });
    },
    fail: () => {}
  })
}
